<?php
# Part of RASS unittest fixtures: item upload and mgmt.
# Copyright (C) 2008 rambla.be
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

class RassPaginationTest extends UnitTestCase {
    function TestRassClientlib() {
        $this->UnitTestCase("Rass Pagination Tests");
    }

    # we don't use the regular setUp and tearDown functions here, cause this would cause too much traffic
    # instead we're relying on the fact that methods are executed in declaration order
    function testSetup() {
      try {
        # Create the following dir structure:
        # /
        # /FILENAME
        # /DIRNAME
        # /DIRNAME/FILENAME
        // $rass = new Rass(SUPERUSER, SUPERPWD, BASE);
        // $newUser = $rass->createUser(USER, EMAIL, PWD);
        # create a subdir
        $rass = new Rass(USER, PWD, BASE);
        $rass->setSSL(SSL);
        $rass->createItem(ITEMNAME, LOCAL_FILE);
        $entry = $rass->createDir(DIRNAME);
        $rass->createItem(DIRNAME . "/" . ITEMNAME, LOCAL_FILE);
        $rass->createItem(DIRNAME . "/" . ITEMNAME, LOCAL_FILE);
        $rass->createItem(DIRNAME . "/" . ITEMNAME, LOCAL_FILE);
        $rass->createItem(DIRNAME . "/" . ITEMNAME, LOCAL_FILE);
        $rass->createItem(DIRNAME . "/" . ITEMNAME, LOCAL_FILE);
        $rass->createItem(DIRNAME . "/" . ITEMNAME, LOCAL_FILE);
        $rass->createItem(DIRNAME . "/" . ITEMNAME, LOCAL_FILE);
        $rass->createItem(DIRNAME . "/" . ITEMNAME, LOCAL_FILE);
        $rass->createItem(DIRNAME . "/" . ITEMNAME, LOCAL_FILE);
        $rass->createItem(DIRNAME . "/" . ITEMNAME, LOCAL_FILE);
        $rass->createItem(DIRNAME . "/" . ITEMNAME, LOCAL_FILE);
        $entry = $rass->createDir(DIRNAME . "/" .DIRNAME, True); # force create
        $entry = $rass->createDir(DIRNAME . "/" .DIRNAME, True); # force create
        $entry = $rass->createDir(DIRNAME . "/" .DIRNAME, True); # force create
        $entry = $rass->createDir(DIRNAME . "/" .DIRNAME, True); # force create
        $entry = $rass->createDir(DIRNAME . "/" .DIRNAME, True); # force create
        $entry = $rass->createDir(DIRNAME . "/" .DIRNAME, True); # force create
        $entry = $rass->createDir(DIRNAME . "/" .DIRNAME, True); # force create
        $entry = $rass->createDir(DIRNAME . "/" .DIRNAME, True); # force create
        $entry = $rass->createDir(DIRNAME . "/" .DIRNAME, True); # force create
        $entry = $rass->createDir(DIRNAME . "/" .DIRNAME, True); # force create
        $entry = $rass->createDir(DIRNAME . "/" .DIRNAME, True); # force create
      }
      catch(Zend_Gdata_App_Exception $e) {
        assert(False);
      }
    }

    function testGetPaginatedFeed() {
      $entriesFound = 0;
      try {
        # check the rootdir
        $rass = new Rass(USER, PWD, BASE);
        $rass->setSSL(SSL);
        $query = new Rass_DirQuery();
        $query->setKind("file");
        $query->setPaginateBy("5");
        $feed = $rass->getDirFeed(DIRNAME, $query);
        $this->assertEqual($rass->getLastNr($feed), 3);
        $this->assertEqual($rass->getLastLink($feed), BASE . "dir/chiquita/?kind=file;page=3;paginate_by=5;");
        $this->assertEqual($rass->getNextLink($feed), BASE . "dir/chiquita/?kind=file;page=2;paginate_by=5;");
        $entries = 0;
        foreach ($feed as $entry) {
            $this->assertEqual($entry->kind, "file");
            $entries += 1;
          }
        $this->assertEqual($entries, 5);
        
        $feed = $rass->getNextFeed($feed);
        $this->assertEqual($rass->getLastNr($feed), 3);
        $this->assertEqual($rass->getLastLink($feed), BASE . "dir/chiquita/?kind=file;page=3;paginate_by=5;");
        $this->assertEqual($rass->getNextLink($feed), BASE . "dir/chiquita/?kind=file;page=3;paginate_by=5;");
        $entries = 0;
        foreach ($feed as $entry) {
            $this->assertEqual($entry->kind, "file");
            $entries += 1;
          }
        $this->assertEqual($entries, 5);

        $feed = $rass->getNextFeed($feed);
        $this->assertEqual($rass->getLastNr($feed), 3);
        $this->assertEqual($rass->getLastLink($feed), BASE . "dir/chiquita/?kind=file;page=3;paginate_by=5;");
        $this->assertEqual($rass->getNextLink($feed), null);
        $entries = 0;
        foreach ($feed as $entry) {
            $this->assertEqual($entry->kind, "file");
            $entries += 1;
          }
        $this->assertEqual($entries, 1);
        
        # check the rootdir
        $rass = new Rass(USER, PWD, BASE);
        $rass->setSSL(SSL);
        $query = new Rass_DirQuery();
        $query->setKind("dir");
        $query->setPaginateBy("5");
        $feed = $rass->getDirFeed(DIRNAME, $query);
        $this->assertEqual($rass->getLastNr($feed), 3);
        $this->assertEqual($rass->getLastLink($feed), BASE . "dir/chiquita/?kind=dir;page=3;paginate_by=5;");
        $this->assertEqual($rass->getNextLink($feed), BASE . "dir/chiquita/?kind=dir;page=2;paginate_by=5;");
        $entries = 0;
        foreach ($feed as $entry) {
            $this->assertEqual($entry->kind, "dir");
            $entries += 1;
          }
        $this->assertEqual($entries, 5);
        
        $feed = $rass->getNextFeed($feed);
        $this->assertEqual($rass->getLastNr($feed), 3);
        $this->assertEqual($rass->getLastLink($feed), BASE . "dir/chiquita/?kind=dir;page=3;paginate_by=5;");
        $this->assertEqual($rass->getNextLink($feed), BASE . "dir/chiquita/?kind=dir;page=3;paginate_by=5;");
        $entries = 0;
        foreach ($feed as $entry) {
            $this->assertEqual($entry->kind, "dir");
            $entries += 1;
          }
        $this->assertEqual($entries, 5);

        $feed = $rass->getNextFeed($feed);
        $this->assertEqual($rass->getLastNr($feed), 3);
        $this->assertEqual($rass->getLastLink($feed), BASE . "dir/chiquita/?kind=dir;page=3;paginate_by=5;");
        $this->assertEqual($rass->getNextLink($feed), null);
        $entries = 0;
        foreach ($feed as $entry) {
            $this->assertEqual($entry->kind, "dir");
            $entries += 1;
          }
        $this->assertEqual($entries, 1);
        
        
      }
      catch(Zend_Gdata_App_Exception $e) {
        assert(False);
      }
    }
    
    
    function testTeardown() {
      try {
        $rass = new Rass(USER, PWD, BASE);
        $rass->setSSL(SSL);
        $rass->deleteItem(ITEMNAME);
        $entry = $rass->deleteDir(DIRNAME, True); # delete recursive
        // $rass = new Rass(SUPERUSER, SUPERPWD, BASE);
        // $newUser = $rass->deleteUser(USER);
      }
      catch(Zend_Gdata_App_Exception $e) {
        assert(False);
      }
    }


}
